//
//  WordsViewController.m
//  RV获取JSON数据_10-19
//
//  Created by 伍银荣 on 2017/10/19.
//  Copyright © 2017年 WYR. All rights reserved.
//

#import "WordsViewController.h"
#import "wordsModel.h"

@interface WordsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) NSArray *wordsArr;
@property (nonatomic,strong) UITableView *myTableView;

@end

@implementation WordsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    NSString *path = [[NSBundle mainBundle]pathForResource:self.key ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    
    NSMutableArray *marr = [NSMutableArray array];
    for (NSDictionary *dict in arr) {
        wordsModel *wordsItem = [[wordsModel alloc]initWithDict:dict];
        [marr addObject:wordsItem];
    }
    _wordsArr = marr;
    NSLog(@"---------wordsArr:%@",_wordsArr);
    
    [self setupUI];
}

-(void)setupUI
{
    [self.view addSubview:self.myTableView];
    //[_myTableView reloadData];
}

-(UITableView *)myTableView
{
    if (!_myTableView) {
        UITableView *myTableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
        myTableView.delegate = self;
        myTableView.dataSource = self;
        _myTableView = myTableView;
    }
    return _myTableView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _wordsArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"myCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    cell.textLabel.text = [_wordsArr[indexPath.row] Word_Key];
    cell.detailTextLabel.text = [_wordsArr[indexPath.row] Word_Trans];
    return cell;
}
    


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
